﻿using System.Data.SqlClient;
using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SoftballStats_BL
{
    public partial class Season
    {
#region Other Properties
        private Guid _ID;
        private int _Year;
        private string _Description;

        public Guid ID
        {
            get
            {
                return _ID;
            }
            set
            {
                _ID = value;
            }
        }

        public int Year
        {
            get
            {
                return _Year;
            }
            set
            {
                _Year = value;
            }
        }

        public string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                _Description = value;
            }
        }
#endregion

#region Public Methods
        public static List<Season> GetSeasons()
        {
            List<SoftballStats_BL.Season> lstSeasons = new List<SoftballStats_BL.Season>();

            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetSeasons";
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            Season thisSeason = new Season();
                            thisSeason.ID = Guid.Parse(dr["ID"].ToString());
                            thisSeason.Year = int.Parse(dr["Year"].ToString());
                            thisSeason.Description = dr["Description"].ToString();
                            lstSeasons.Add(thisSeason);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get Seasons", ex);
            }

            return lstSeasons;
        }

        public static Season GetSeasonByID(string SeasonID)
        {
            Season thisSeason = new Season();
            thisSeason.Year = -1;

            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetSeasonByID";
                    cmd.Parameters.AddWithValue("@SeasonID", SeasonID);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            thisSeason.ID = Guid.Parse(dr["ID"].ToString());
                            thisSeason.Year = int.Parse(dr["Year"].ToString());
                            thisSeason.Description = dr["Description"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get Season", ex);
            }

            return thisSeason;
        }

        public static Season GetCurrentSeason()
        {
            Season thisSeason = new Season();
            thisSeason.Year = -1;

            try
            {
                using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.CommandText = "spGetCurrentSeason";
                    if (DateTime.Now.Month > 7)
                    {
                        cmd.Parameters.AddWithValue("@Season", "Fall");
                    }
                    else
                    {
                        cmd.Parameters.AddWithValue("@Season", "Spring");
                    }
                    cmd.Parameters.AddWithValue("@Year", DateTime.Now.Year);
                    conn.Open();
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read())
                        {
                            thisSeason.ID = Guid.Parse(dr["ID"].ToString());
                            thisSeason.Year = int.Parse(dr["Year"].ToString());
                            thisSeason.Description = dr["Description"].ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException("Failed to get current Season", ex);
            }

            return thisSeason;
        }

        //public static Team GetTeamByName(string teamName)
        //{
        //    Team thisTeam = new Team();
        //    try
        //    {
        //        using (SqlConnection conn = new SqlConnection(Utility.GetConnectiongString()))
        //        {
        //            SqlCommand cmd = new SqlCommand();
        //            cmd.Connection = conn;
        //            cmd.CommandType = CommandType.StoredProcedure;
        //            cmd.CommandText = "spGetTeamByName";
        //            cmd.Parameters.AddWithValue("@Name", teamName);
        //            conn.Open();
        //            using (SqlDataReader dr = cmd.ExecuteReader())
        //            {
        //                while (dr.Read())
        //                {
        //                    thisTeam.ID = Guid.Parse(dr["ID"].ToString());
        //                    thisTeam.Name = dr["Name"].ToString();
        //                    thisTeam.HeadCoach = dr["HeadCoach"].ToString();
        //                }
        //            }
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new ApplicationException("Failed to get team", ex);
        //    }
        //    return thisTeam;
        //}
#endregion
    }
}
